package com.aqiang.day0528_shop.main.view.activity

import android.content.ContentValues
import android.os.Bundle
import android.view.View
import com.aqiang.day0528_shop.R
import com.aqiang.day0528_shop.main.contract.AreaContract
import com.aqiang.day0528_shop.sql.UserInfo
import com.aqiang.lib_core.mvp.BaseActivity
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import kotlinx.android.synthetic.main.activity_add.*
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import com.alibaba.fastjson.JSON
import com.aqiang.day0528_shop.main.entity.Province
import com.aqiang.lib_core.common.SLog
import org.jetbrains.anko.doAsync


class AddActivity : BaseActivity<AreaContract.AreaPresenter>(){
    private var userInfo: UserInfo? = null
    private var options1Items:MutableList<String>? = null
    private var options2Items:MutableList<MutableList<String>>? = null
    private var options3Items:MutableList<MutableList<MutableList<String>>>? = null
    override fun bindLatout(): Int {
        return R.layout.activity_add
    }

    override fun createPresenter() {
        ///mPresenter = AreaPresenter(this)
    }

    override fun initView(savedInstanceState: Bundle?) {

    }

    override fun initData() {
        options1Items = mutableListOf()
        options2Items = mutableListOf()
        options3Items = mutableListOf()
//        doAsync {
//
//        }

        doAsynTask {
           //SLog.d("11")
            val json = getJson()
            val provinces = JSON.parseArray(json, Province::class.java)
            for (item in provinces.indices) {
                //SLog.d("22")
                val name = provinces.get(item).getName()
                options1Items!!.add(name)
                val cityList = mutableListOf<String>()
                val areList =  mutableListOf<MutableList<String>>()
                val city = provinces.get(item).getCity()
                for (cityBean in city) {
                    cityList.add(cityBean.getName())
                    areList.add(cityBean.getArea())
                    SLog.d("${options1Items!!.size}")
                }
                options2Items!!.add(cityList)
                options3Items!!.add(areList)
                //SLog.d("${options2Items!!.size}")
                //SLog.d("${options3Items!!.size}")
            }
        }
        //SLog.d("${options1Items!!.size}")
        //SLog.d("${options2Items!!.size}")
        //SLog.d("${options3Items!!.size}")
        userInfo = UserInfo(this)
    }

    override fun initEvent() {
        tv_act_add_area.setOnClickListener {
            val optionsPickerView = OptionsPickerBuilder(this,object :OnOptionsSelectListener{
                override fun onOptionsSelect(options1: Int, options2: Int, options3: Int, v: View?) {

                }
            }).build<String>()
            optionsPickerView?.setPicker(options1Items,options2Items,options3Items)
            optionsPickerView.show()
        }

        bt_act_add_save.setOnClickListener {
            userInfo!!.use {
                val contentValues = ContentValues()
                contentValues.put("name",et_act_add_name.text.toString())
                contentValues.put("tel",et_act_add_tel.text.toString())
                contentValues.put("area",et_act_add_a.text.toString())
                contentValues.put("address",et_act_add_address.text.toString())
                insert(UserInfo.sqlName,null,contentValues)
            }
        }
    }

    private fun getJson(): String {
        val stringBuffer = StringBuffer()
        val assetManager = assets
        try {
            val br = BufferedReader(InputStreamReader(assetManager.open("province.json")))
            var line:String = ""
            while (({ line = br.readLine();line }) != null) {
                //SLog.d("${br.readLine()}")
                stringBuffer.append(line)
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }

        return stringBuffer.toString()
    }

//    override fun area(data: Deferred<BaseEntity<List<AreaEntity>>>) {
//        doUiThread(data,{
//            val data1 = it.data
//            for (item in 1..data1.size - 1){
//                doAsynTask {
//                    mPresenter!!.getArea1(data1.get(item).Id)
//                }
//                options1Items!!.add(data1.get(item).Name)
//            }
//            //SLog.d("${options1Items!!.size}")
//        })
//    }
//
//    override fun area1(data: Deferred<BaseEntity<List<AreaEntity>>>) {
//        doUiThread(data,{
//            val data1 = it.data
//            var cityList:MutableList<String> = mutableListOf()
//            for (item in data1.indices){
//                doAsynTask {
//                    mPresenter!!.getArea3(data1.get(item).Id)
//                }
//                cityList.add(data1.get(item).Name)
//            }
//            options2Items!!.add(cityList)
//        })
//    }
//
//    override fun area2(data: Deferred<BaseEntity<List<AreaEntity>>>) {
//       doUiThread(data,{
//           val data1 = it.data
//
//       })
//    }
}
